<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>FooTable</title>

    <link rel="shortcut icon" href="favicon.ico"> <link href="css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="css/plugins/footable/footable.core.css" rel="stylesheet">

    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css?v=4.1.0" rel="stylesheet">

</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
		<div class="row">
			<div class="col-sm-12">
				<div class="ibox float-e-margins">
					<div class="ibox-title">
						<h5>角色与权限查询</h5>
					</div>
					<div class="ibox-content">
						<table id="roleTable" class="table">
							<thead>
							<tr>
								<th>角色id</th>
								<th>角色名称</th>
								<th>角色类型</th>
								<th>角色代码</th>
								<th>所属子系统</th>
								<th>角色权限</th>
								<th>操作</th>
								<th></th>
							</tr>
							</thead>
							<tbody>
							</tbody>
							<tfoot>
							</tfoot>
						</table>
						<div>
							<button id="prevButton" type="button" class="btn btn-primary">上一页</button>
							<button id="nextButton" type="button" class="btn btn-primary">下一页</button>
						</div>
					</div>
					<div id="permissionsTable" class="ibox float-e-margins">
						<div class="ibox-title">
							<h5>权限修改 <span id="currentPermission"></span></h5>
						</div>
						<div class="ibox-content">
							<table id="permissionTable" class="table">
								<thead>
								<tr>
									<th>权限id</th>
									<th>权限内容</th>
									<th>权限</th>
									<th></th>
								</tr>
								</thead>
								<tbody>
								</tbody>
								<tfoot>
								</tfoot>
							</table>
							<form class="form-horizontal">
								<div class="form-group">
									<div class="col-sm-2">
										<button id="permissions-submit" class="btn btn-primary">确认权限修改</button>
									</div>
								</div>
							</form>
						</div>
					</div>
				
					
					<div id="modify-form" class="ibox float-e-margins">
						<div class="ibox-title">
							<h5>修改角色</h5>
						</div>
						
						<div class="ibox-content">
							<form class="form-horizontal">
								<!--p>新增行政区域</p-->
								<div class="form-group">
									<label class="col-sm-2 control-label">角色id：</label>
									<div class="col-sm-4">
										<input id="modify-roleId" readonly="readonly" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">角色代码：</label>
									<div class="col-sm-4">
										<input id="modify-roleCode" placeholder="角色代码" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">角色名称：</label>
									<div class="col-sm-4">
										<input id="modify-roleName" placeholder="角色名称" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">角色类型：</label>
									<div class="col-sm-4">
										<input id="modify-roleRemark" placeholder="角色类型" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">所属子系统：</label>
                                    <div class="col-sm-6">
										<div class="radio">
											<label>
												<input type="radio" value="1" id="modify_Radio1" name="modifyRadios">公路养护管理子系统</label>
										</div>
										<div class="radio">
											<label>
												<input type="radio" value="2" id="modify_Radio2" name="modifyRadios">桥梁隧道管理子系统</label>
										</div>
										<div class="radio">
											<label>
												<input type="radio" value="3" id="modify_Radio3" name="modifyRadios">农村公路管理子系统</label>
										</div>
										<div class="radio">
											<label>
												<input type="radio" value="4" id="modify_Radio4" name="modifyRadios">公路交通运行动态监测与分析子系统</label>
										</div>
                                    </div>
								</div>
								<div class="form-group">
									<div class="col-sm-2">
										<button id="modify-submit" class="btn btn-primary">确认修改</button>
									</div>
								</div>
							</form>
						</div>
					</div>
			
					<div class="ibox float-e-margins">
						<div class="ibox-title">
							<h5>新增角色</h5>
							<div class="ibox-content">
									<button id="addButton" type="button" class="btn btn-primary">新增</button>
							</div>
						</div>
						
						<div id="add-form" class="ibox-content">
							<form class="form-horizontal">
								<!--p>新增行政区域</p-->
								<div class="form-group">
									<label class="col-sm-2 control-label">角色代码：</label>
									<div class="col-sm-4">
										<input id="add-roleCode" placeholder="角色代码" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">角色名称：</label>
									<div class="col-sm-4">
										<input id="add-roleName" placeholder="角色名称" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">角色类型：</label>
									<div class="col-sm-4">
										<input id="add-roleRemark" placeholder="角色类型" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">所属子系统：</label>
                                    <div class="col-sm-6">
										<div class="radio">
										<label>
											<input type="radio" checked="true" value="1" id="add_Radio1" name="addRadios">公路养护管理子系统</label>
										</div>
										<div class="radio">
											<label>
												<input type="radio" value="2" id="add_Radio2" name="addRadios">桥梁隧道管理子系统</label>
										</div>
										<div class="radio">
											<label>
												<input type="radio" value="3" id="add_Radio3" name="addRadios">农村公路管理子系统</label>
										</div>
										<div class="radio">
											<label>
												<input type="radio" value="4" id="add_Radio4" name="addRadios">公路交通运行动态监测与分析子系统</label>
										</div>
                                    </div>
								</div>
								<div class="form-group">
									<div class="col-sm-offset-2 col-sm-4">
										<button id="add-submit" class="btn btn-primary">确认新增</button>
									</div>
								</div>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	
    <!-- 全局js -->
    <script src="js/jquery.min.js?v=2.1.4"></script>
    <script src="js/bootstrap.min.js?v=3.3.6"></script>
    <script src="js/plugins/footable/footable.all.min.js"></script>
    <!-- 自定义js -->
    <!--script src="js/content.js?v=1.0.0"></script-->
	<script src="js/service/service.js"></script>
    <script>
		let currPage;
		let PageSize;
		let PermissionRoldId;
        $(document).ready(function() {
			currPage = 1
			PageSize = 5
			PermissionRole = 0
			$("div#add-form").hide()
			$('#modify-form').hide()
			$("div#permissionsTable").hide()
			service.getRoleBypage(currPage, PageSize, fillRole, console.log)
        });
		
		$('.footable').footable();
		function fillRole(obj){
			var totalpage = JSON.parse(obj)["totalpage"]
			if(currPage == totalpage || totalpage == 0){
				$('#nextButton').hide()
			}else{
				$('#nextButton').show()
			}
			
			if(currPage == 1 || totalpage == 0){
				$('#prevButton').hide()
			}else{
				$('#prevButton').show()
			}
		
			var objValue = JSON.parse(obj)["data"]
			var levelArray = ["", "公路养护管理子系统", "桥梁隧道管理子系统", "农村公路管理子系统", "公路交通运行动态监测与分析子系统"]
			var table = document.getElementsByTagName("tbody")[0];
			for(var ele in objValue){
				var tr = table.insertRow(table.rows.length);
				tr.insertCell(0).innerHTML = objValue[ele]["id"]
				tr.insertCell(1).innerHTML = objValue[ele]["roleName"]
				tr.insertCell(2).innerHTML = objValue[ele]["remark"]
				tr.insertCell(3).innerHTML = objValue[ele]["roleCode"]
				var levelCode = objValue[ele]["childP"]
				if(levelCode == 1 || levelCode == 2 || levelCode == 3 || levelCode == 4){
					tr.insertCell(4).innerHTML = levelArray[objValue[ele]["childP"]]
				}else{
					tr.insertCell(4).innerHTML = objValue[ele]["childP"]
				}
				tr.insertCell(5).innerHTML = '<a onclick="permission(' + objValue[ele]["id"] +')"><i class="fa fa-wrench"></i> 权限</a>';
				tr.insertCell(6).innerHTML = '<a onclick="modify(this,' + objValue[ele]["id"] +')"><i class="fa fa-wrench"></i> 修改</a>';
				tr.insertCell(7).innerHTML = '<a onclick="remove(this,' + objValue[ele]["id"] +')"><i class="fa fa-wrench"></i> 删除</a>';
			}	
		}

		function permission(id){
			PermissionRole = id
			service.getPermission(id, fillPermission, console.log)
		}
		
		function fillPermission(obj){
			$("div#permissionsTable").show()
			$("#permissionsTable tbody").html("")
			var objValue = JSON.parse(obj)["data"]
			var index = 0
			var table = document.getElementsByTagName("tbody")[1];
			for(var ele in objValue){
				if(objValue[ele]["funOrMenu"] == 2){
					index++;
					var tr = table.insertRow(table.rows.length);
					tr.insertCell(0).innerHTML = objValue[ele]["id"]
					tr.insertCell(1).innerHTML = objValue[ele]["menuName"]
					tr.insertCell(2).innerHTML = '<div class="radio"><label><input type="radio" value="true" id="perRadioTrue'+ index +'" name="per'+ index +'">有权限</label><label><input type="radio" value="false" id="perRadioFalse'+ index +'" name="per'+ index +'" >没有权限</label></div>';
					if(objValue[ele]["select"] == true){
						$('#perRadioTrue'+ele).prop('checked', true)
					}else{
						$('#perRadioFalse'+ele).prop('checked', true)
					}
				}
			}
		}
		
		$('#permissions-submit').click(function(){
			var list = "(["
			var tb = document.getElementById('permissionTable');
			var rows = tb.rows;
			for(var i = 1; i<rows.length; i++ ){
				var id = rows[i].cells[0].innerHTML
				var select = $('#permissionsTable input[name="per' + i +'"]:checked ').val()
				list += "{id:"+id+",select:"+select+"}"
				if(i < rows.length-1)
					list += ","
			}
			list += "])"
			service.modifyRolePermission(PermissionRole, eval(list), help, console.log)
		});
		
		function help(obj){
			console.log(obj)
			alert('操作成功')
		}
		
		function remove(cell,id){
			service.deleteRole(id, help, console.log)
			var tr=cell.parentNode.parentNode;
			var table=document.getElementsByTagName("table")[0]
			table.deleteRow(tr.rowIndex);
		}
		
		function modify(cell, id){
			$('#modify-form').show()
			var tr = cell.parentNode.parentNode;
			$('#modify-roleId').attr('value',tr.cells[0].innerText)
			$('#modify-roleName').attr('value',tr.cells[1].innerText)
			$('#modify-roleRemark').attr('value',tr.cells[2].innerText)
			$('#modify-roleCode').attr('value',tr.cells[3].innerText)
		
			var levelArray = ["", "公路养护管理子系统", "桥梁隧道管理子系统", "农村公路管理子系统", "公路交通运行动态监测与分析子系统"]
			var level = tr.cells[4].innerText
			
			if(level == levelArray[1]){
				$("input[name='modifyRadios']:eq(0)").prop("checked",'checked'); 
			}else if(level == levelArray[2]){
				$("input[name='modifyRadios']:eq(1)").prop("checked",'checked'); 
			}else if(level == levelArray[3]){
				$("input[name='modifyRadios']:eq(2)").prop("checked",'checked'); 
			}else if(level == levelArray[4]){
				$("input[name='modifyRadios']:eq(3)").prop("checked",'checked'); 
			}
		}
		
		$('#modify-submit').click(function(){
			var id, code, name, level, remark
			id = $('#modify-roleId').val()
			code = $('#modify-roleCode').val()
			name = $('#modify-roleName').val()
			remark = $('#modify-roleRemark').val()
			level = 1
			if($('#modify_Radio1').prop("checked")){
				level = $('#modify_Radio1').val()
			}else if($('#modify_Radio2').prop("checked")){
				level = $('#modify_Radio2').val()
			}else if($('#modify_Radio3').prop("checked")){
				level = $('#modify_Radio3').val()
			}else if($('#modify_Radio4').prop("checked")){
				level = $('#modify_Radio4').val()
			}
			if(name == "" || code == "" || remark == ""){
				alert("请完整填写内容")
			}else{
				service.modifyRole(id, code, name, level, remark, help, console.log)
			}
		});
		
		$('#add-submit').click(function(){
			var code, name, level, remark
			code = $('#add-roleCode').val()
			name = $('#add-roleName').val()
			remark = $('#add-roleRemark').val()
			level = 1
			if($('#add_Radio1').prop("checked")){
				level = $('#add_Radio1').val()
			}else if($('#add_Radio2').prop("checked")){
				level = $('#add_Radio2').val()
			}else if($('#add_Radio3').prop("checked")){
				level = $('#add_Radio3').val()
			}else if($('#add_Radio4').prop("checked")){
				level = $('#add_Radio4').val()
			}
			alert(level)
			if(name == "" || code == "" || remark == ""){
				alert("请完整填写内容")
			}else{
				service.addRole(code, name, level, remark, help, console.log)
			}
		});
		
		function add_help(obj){
			console.log(obj)
			alert('添加成功')
		}
		
		$('#nextButton').click(function(){
			$("#roleTable tbody").html("")
			currPage = currPage + 1
			service.getRoleBypage(currPage, PageSize, fillRole, console.log)
		});
		
		$('#prevButton').click(function(){
			$("#roleTable tbody").html("")
			currPage = currPage - 1
			service.getRoleBypage(currPage, PageSize, fillRole, console.log)
		});
		
		$('#addButton').click(function(){
			if($('div#add-form').is(':hidden')){
				$('div#add-form').show()
				$('#addButton').text("取消新增")
			}else{
				$('div#add-form').hide()
				$('#addButton').text("新增")
			}
		});
		
		
    </script>

    <script type="text/javascript" src="http://tajs.qq.com/stats?sId=9051096" charset="UTF-8"></script>
    <!--统计代码，可删除-->

</body>

</html>
